import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:kiss/controller/note_index_ctl.dart';

class AddNoteController extends GetxController {
  final NoteIndexController homeController = Get.find(); // 获取 HomeController 实例

  // 标题和内容的控制器
  final TextEditingController titleController = TextEditingController();
  final TextEditingController contentController = TextEditingController();

  // 当前选中的分类
  final RxString selectedCategory = 'Work'.obs;

  // 添加笔记
  void addNote() {
    final title = titleController.text;
    final content = contentController.text;

    if (title.isNotEmpty && content.isNotEmpty) {
      final now = DateTime.now();
      homeController.addNote(
        title: title,
        content: content,
        category: selectedCategory.value,
        createdAt: now,
        updatedAt: now,
      );
      Get.back(); // 返回上一页
    } else {
      Get.snackbar('Error', 'Title and content cannot be empty'); // 提示用户输入不能为空
    }
  }

  @override
  void onClose() {
    // 释放资源
    titleController.dispose();
    contentController.dispose();
    super.onClose();
  }
}